package com.lmabbe.common.mvc.core.swagger;

import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import com.lmabbe.common.global.constant.GlobalConstant;
import com.lmabbe.common.mvc.config.LmabbeMvcProperties;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import springfox.documentation.builders.*;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.ParameterType;
import springfox.documentation.service.RequestParameter;
import springfox.documentation.service.Response;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

import java.util.ArrayList;
import java.util.List;

/**
 * @author lmabbe
 * @data 2021/3/25 20:36
 */
@Configuration
@EnableOpenApi
@EnableKnife4j
@RequiredArgsConstructor
@ConditionalOnProperty(prefix = "lmabbe.mvc.swagger", name = "enable", havingValue = "true", matchIfMissing = true)
public class LmabbeSwagger3AutoConfiguration {

    private final LmabbeMvcProperties lmabbeMvcProperties;

    /**
     * swagger3的配置文件
     */
    @Bean
    @ConditionalOnMissingBean
    public Docket createRestApi() {
        return new Docket(DocumentationType.OAS_30)
                .apiInfo(apiInfo())
                .pathMapping(lmabbeMvcProperties.getSwagger().getServiceUrl())
                .select()
                .apis(RequestHandlerSelectors.withMethodAnnotation(Operation.class))
                .apis(RequestHandlerSelectors.basePackage(lmabbeMvcProperties.getSwagger().getScanPackage()))
                .paths(PathSelectors.any())
                .build()
                .enable(lmabbeMvcProperties.getSwagger().isEnable())
                .globalRequestParameters(getGlobalRequestParameters())
                .globalResponses(HttpMethod.GET, getGlobalResponseMessage())
                .globalResponses(HttpMethod.POST, getGlobalResponseMessage())
                .globalResponses(HttpMethod.DELETE, getGlobalResponseMessage())
                .globalResponses(HttpMethod.PUT, getGlobalResponseMessage());
    }

    /**
     * 构建 api文档的详细信息函数,注意这里的注解引用的是哪个
     */
    private ApiInfo apiInfo() {
        // 获取工程名称
        return new ApiInfoBuilder()
                .title(lmabbeMvcProperties.getSwagger().getTitle())
                .contact(lmabbeMvcProperties.getSwagger().getContact())
                .version(lmabbeMvcProperties.getSwagger().getVersion())
                .description(lmabbeMvcProperties.getSwagger().getDesc())
                .build();
    }

    /**
     * 生成全局通用参数
     *
     * @return
     */
    private List<RequestParameter> getGlobalRequestParameters() {
        List<RequestParameter> parameters = new ArrayList<>();
        parameters.add(new RequestParameterBuilder()
                .name(GlobalConstant.SYSTEM.AUTHORIZATION)
                .description("令牌")
                .required(true)
                .in(ParameterType.HEADER)
                .build());
        parameters.add(new RequestParameterBuilder()
                .name(GlobalConstant.SYSTEM.TENANT_ID)
                .description("租户ID")
                .required(true)
                .in(ParameterType.HEADER)
                .build());
        parameters.add(new RequestParameterBuilder()
                .name(GlobalConstant.SYSTEM.FROM_CLIENT)
                .description("客户端")
                .required(true)
                .in(ParameterType.HEADER)
                .build());
        return parameters;
    }

    /**
     * 生成通用响应信息
     *
     * @return
     */
    private List<Response> getGlobalResponseMessage() {
        List<Response> responseList = new ArrayList<>();
        responseList.add(new ResponseBuilder().code("404").description("找不到资源").build());
        return responseList;
    }

}
